package com.tedu.dao.impl;

import com.tedu.dao.UserDao;
import com.tedu.entity.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao {

    @Override
    public User login(User user) {
        User user1 = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/ankang"
                    , "root", "root");
            String sql = "select * from user where username = ? and password=?";
            PreparedStatement pstm = conn.prepareStatement(sql);
            pstm.setString(1, user.getUsername());
            pstm.setString(2, user.getPassword());
            ResultSet rs = pstm.executeQuery();
            if(rs.next()){
                user1 = new User(
                                rs.getInt(1),
                                rs.getString(2),
                                rs.getString(3),
                                rs.getString(4),
                                rs.getString(5)
                        );
            }
            rs.close();
            pstm.close();
            conn.close();


        } catch (Exception e) {
            e.printStackTrace();
        }

        return user1;
    }
}
